package com.sandking.db.jedis;

import com.sandking.db.bean.Xxzxbb;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import com.sandking.db.dao.XxzxbbDao;
import java.util.List;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class XxzxbbJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Xxzxbb getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Xxzxbb xxzxbb = null;
		try{
			String key = "Xxzxbb_Object";
			String field = SK_Plus.b("id:",id).e();
			xxzxbb = Xxzxbb.createForJson(jedis.hget(key,field));
			if(xxzxbb == null){
				xxzxbb = XxzxbbDao.getById(id);
				if(xxzxbb!=null){
					loadCache(xxzxbb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return xxzxbb;
	}
	

	public static void loadCache(Xxzxbb xxzxbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(xxzxbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Xxzxbb xxzxbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Xxzxbb_Object";
		String field = SK_Plus.b("id:",xxzxbb.getId()).e();
		String data = xxzxbb.toJson();
		p.hset(key,field,data);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Xxzxbb> xxzxbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Xxzxbb xxzxbb : xxzxbbs){
				loadCache(xxzxbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Xxzxbb xxzxbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(xxzxbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Xxzxbb xxzxbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Xxzxbb_Object";
		String field = SK_Plus.b("id:",xxzxbb.getId()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Xxzxbb> xxzxbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Xxzxbb xxzxbb : xxzxbbs){
				clearCache(xxzxbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Xxzxbb insert(Xxzxbb xxzxbb){
		xxzxbb = XxzxbbDao.insert(xxzxbb);
		if(xxzxbb!=null){
			loadCache(xxzxbb);
		}
    	return xxzxbb;
    }
    
    public static Xxzxbb update(Xxzxbb xxzxbb){
    	xxzxbb = XxzxbbDao.update(xxzxbb);
    	if(xxzxbb!=null){
			loadCache(xxzxbb);
		}
    	return xxzxbb;
    }
    
    public static boolean delete(Xxzxbb xxzxbb){
    	boolean bool = XxzxbbDao.delete(xxzxbb);
    	if(bool){
    		clearCache(xxzxbb);
    	}
    	return bool;
    }
}